<%@ page import="com.party.core.model.system.TargetType" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../../include/tag.jsp" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>${project == null ? '新增' : '编辑'}老师</title>
    <%@include file="../../include/commonFile.jsp" %>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/common/form.css">
    <%--<link rel="stylesheet" href="${ctx}/css/ui/activity/publish_form.css">--%>
	<style type="text/css">
		.index-outside > section .section-main {
			min-width: 0px !important;
			padding: 15px;
		}

		.index-outside > section {
			padding-bottom: 0px !important;
		}
	</style>

</head>
<body>
<!--头部-->
<div class="index-outside">
    <!--内容-->
    <section class="no-fix">
        <div class="section-main" q>
            <!-- 正文请写在这里 -->
            <div class="add-form-content">
                <form id="myForm" class="layui-form mt20" method="post" action="${ctx}/school/teacher/teacherSave.do">

                    <div class="layui-form-item">
                        <label class="layui-form-label">老师名称<span class="f-verify-red">*</span></label>
                        <div class="layui-input-inline">
                            <input type="text" name="name" lay-verify="required" placeholder="请输入老师名称"
                                   class="layui-input" value="${teacher.name}">
                            <input type="hidden" name="id" value="${teacher.id}"/>
                        </div>
                    </div>
                    <%--<div class="layui-form-item">
                        <label class="layui-form-label">封面图<span class="f-verify-red">*</span></label>
                        <div class="cover-content">
                            <input type="hidden" name="picture" id="pic" lay-verify="required" value="${project.picture}" />
                            <c:if test="${project == null || empty project.picture}">
                                <span id="cover-img" class="cover-img" style="background-image:url(${ctx}/image/posterImg.png)"></span>
                            </c:if>
                            <c:if test="${project != null && not empty project.picture}">
                                <span id="cover-img" class="cover-img" style="background-image:url('${project.picture}')"></span>
                            </c:if>
                            <a class="layui-btn layui-btn-danger" id="sel_pic_img">+添加海报</a>
                            <div class="form-word-aux">建议尺寸：800x450</div>
                        </div>
                    </div>--%>
                    <div class="layui-form-item">
                        <label class="layui-form-label">出生日期<span class="f-verify-red">*</span></label>
                        <div class="layui-input-inline">
                            <input class="layui-input" name="birthday" lay-verify="birthday" type="text" readonly
                                   value='<fmt:formatDate value="${teacher.birthday}" pattern="yyyy-MM-dd" />'
                            />
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">年&emsp;&emsp;龄<span class="f-verify-red">*</span></label>
                        <div class="layui-input-inline">
                            <input type="text" name="age" value="${teacher.name}" readonly class="layui-input"/>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <!--底部-->
    </section>
</div>
<script>
    function verify() {
        let flag = true;
        let name = $("[name=name]").val();
        let birthday = $("[name=birthday]").val();
        let age = $("[name=age]").val();
        if (!util.isValid(name)) {
            util.layerMsgError('名称不能为空');
            flag = false;
            return flag;
        }
        if (!util.isValid(birthday)) {
            util.layerMsgError('请选择出生日期');
            flag = false;
            return flag;
        }
        if (!util.isValid(age)) {
            util.layerMsgError('请选择出生日期');
            flag = false;
            return flag;
        }
        return flag;
    }

    function doSubmit() {
        if (verify()) {
            $.ajax({
                type: 'POST',
                async: false, // 使用同步的方法
                data: $('#myForm').serialize(),
                dataType: 'json',
                success: function (res) {
                    if (res.success) {
                        util.layerMsgSuccess("提交成功")
                    } else {
                        util.layerMsgError(res.description)
                    }
                },
                url: $("#myForm").attr('action')
            });
        }
    }


    $(function () {
        layui.use(['form', 'laydate'], function () {
            let laydate = layui.laydate;

            //日期
            laydate.render({
                elem: 'input[name=birthday]',
                max: new Date().Format("yyyy-MM-dd"),
                type: 'date',
                format: 'yyyy-MM-dd',
                done: function (value, date, endDate) {
                    $("[name=age]").val(getAge(value));
                }
            });
        });

        $('#sel_pic_img').click(function () {
            txz.openSelImg({
                min: 1,
                max: 1,
                info: '建议尺寸：800x450',
                type: '<%=TargetType.COMPETITION.getCode()%>',
                cb: function (imgs) {
                    $('#cover-img').css('background-image', 'url(' + imgs[0].path + ')');
                    $('#pic').val(imgs[0].path);
                }
            })
        })

        function getAge(strAge) {
            var birArr = strAge.split("-");
            var birYear = birArr[0];
            var birMonth = birArr[1];
            var birDay = birArr[2];

            d = new Date();
            var nowYear = d.getFullYear();
            var nowMonth = d.getMonth() + 1; //记得加1
            var nowDay = d.getDate();
            var returnAge;

            if (birArr == null) {
                return false
            }
            var d = new Date(birYear, birMonth - 1, birDay);
            if (d.getFullYear() == birYear && (d.getMonth() + 1) == birMonth && d.getDate() == birDay) {
                if (nowYear == birYear) {
                    returnAge = 0; //
                } else {
                    var ageDiff = nowYear - birYear; //
                    if (ageDiff > 0) {
                        if (nowMonth == birMonth) {
                            var dayDiff = nowDay - birDay; //
                            if (dayDiff < 0) {
                                returnAge = ageDiff - 1;
                            } else {
                                returnAge = ageDiff;
                            }
                        } else {
                            var monthDiff = nowMonth - birMonth; //
                            if (monthDiff < 0) {
                                returnAge = ageDiff - 1;
                            } else {
                                returnAge = ageDiff;
                            }
                        }
                    } else {
                        return "出生日期晚于今天，数据有误"; //返回-1 表示出生日期输入错误 晚于今天
                    }
                }
                return returnAge;
            } else {
                return ("输入的日期格式错误！");
            }
        }
    })
</script>
</body>
</html>